package com.yuw.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.yuw.bean.AdminInfoBean;

/**
 * Servlet implementation class AdminInfoAjaxQueryJsonpAction
 */
@WebServlet("/AdminInfoAjaxQueryJsonpAction")
public class AdminInfoAjaxQueryJsonpAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 解决中文乱码问题
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		// 进行ajax处理，进行跨域处理
		// 获取回调函数名
		String strCallBackMethodName = request.getParameter("callback");

		// 获取其他的form表单参数
		String strUserName = request.getParameter("adminname");
		System.out.println("form表单参数:" + strUserName);
		// 进行业务逻辑处理，查询结果信息
		List<AdminInfoBean> lsts = new ArrayList<>();
		// 创建一个实体类对象，模拟从数据库查询到的数据
		AdminInfoBean adminB = new AdminInfoBean();
		adminB.setAdminname("张酸钠");
		adminB.setAdminage(21);
		adminB.setAmdinaddress("天津西青");
		lsts.add(adminB);
		// 使用fastJson将查询结果集List转换为json字符串

		String strJson = JSON.toJSONString(lsts);
		// 写回客户端浏览器
		PrintWriter pw = response.getWriter();
		// jsonp的返回结果是 ： callback +"("+strJson+")";
		// jsonp返回结果类似于： 函数名(json字符串);
		System.out.println("jsonpppp::::" + strCallBackMethodName + "(" + strJson + ");");
		pw.write(strCallBackMethodName + "(" + strJson + ");");
		pw.flush();
		pw.close();
	}

}
